/**
 * @(#)EmailServer.java 2010-2-22
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

import com.wilmar.process.enumerate.EnumEmailServerProperties;

/**
 * 邮件服务器配置信息实体类
 * 
 * @author Administrator Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class EmailServer implements Serializable {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 4400124867232757695L;

	private String smtpHost;
	private Integer smtpPort;
	private String from;
	private Boolean debug;
	private Boolean auth;
	private String user;
	private String password;

	private Boolean mimeAddressStrict;
	private String protocolClass;
	private String protocolHost;
	private Integer protocolPort;
	private String protocolUser;

	private String storeProtocol;
	private String transportProtocol;

	/**
	 * 完整构造函数
	 * 
	 * @param propertieFile
	 *            邮件服务器配置文件
	 */
	public EmailServer(File propertieFile) {
		Properties props = new Properties();
		InputStream inStream;
		try {
			inStream = new FileInputStream(propertieFile);
			props.load(inStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		new EmailServer(props);
	}

	/**
	 * 完整构造函数
	 * 
	 * @param inStream
	 *            邮件服务器配置文件输入流
	 */
	public EmailServer(InputStream inStream) {
		Properties props = new Properties();
		try {
			props.load(inStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.init(props);
	}

	private void init(Properties props) {
		if (props
				.containsKey(EnumEmailServerProperties.DEBUG.getPropertyName())) {
			this.debug = Boolean.valueOf(props
					.getProperty(EnumEmailServerProperties.DEBUG
							.getPropertyName()));
		}
		if (props.containsKey(EnumEmailServerProperties.FROM.getPropertyName())) {
			this.from = props.getProperty(EnumEmailServerProperties.FROM
					.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.MIMEADDRESSSTRICT
				.getPropertyName())) {
			this.mimeAddressStrict = Boolean.valueOf(props
					.getProperty(EnumEmailServerProperties.MIMEADDRESSSTRICT
							.getPropertyName()));
		}
		if (props.containsKey(EnumEmailServerProperties.PROTOCOL_CLASS
				.getPropertyName())) {
			this.protocolClass = props
					.getProperty(EnumEmailServerProperties.PROTOCOL_CLASS
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.PROTOCOL_HOST
				.getPropertyName())) {
			this.protocolHost = props
					.getProperty(EnumEmailServerProperties.PROTOCOL_HOST
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.PROTOCOL_PORT
				.getPropertyName())) {
			this.protocolPort = Integer.parseInt(props
					.getProperty(EnumEmailServerProperties.PROTOCOL_PORT
							.getPropertyName()));
		}
		if (props.containsKey(EnumEmailServerProperties.PROTOCOL_USER
				.getPropertyName())) {
			this.protocolUser = props
					.getProperty(EnumEmailServerProperties.PROTOCOL_USER
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.SMTPHOST_ALIAS
				.getPropertyName())) {
			this.smtpHost = props
					.getProperty(EnumEmailServerProperties.SMTPHOST_ALIAS
							.getPropertyName());
		} else if (props.containsKey(EnumEmailServerProperties.SMTPHOST
				.getPropertyName())) {
			this.smtpHost = props
					.getProperty(EnumEmailServerProperties.SMTPHOST
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.SMTPPORT_ALIAS
				.getPropertyName())) {
			this.smtpPort = Integer.parseInt(props
					.getProperty(EnumEmailServerProperties.SMTPPORT_ALIAS
							.getPropertyName()));
		} else if (props.containsKey(EnumEmailServerProperties.SMTPPORT
				.getPropertyName())) {
			this.smtpPort = Integer.parseInt(props
					.getProperty(EnumEmailServerProperties.SMTPPORT_ALIAS
							.getPropertyName()));
		}
		if (props.containsKey(EnumEmailServerProperties.STORE_PROTOCOL
				.getPropertyName())) {
			this.storeProtocol = props
					.getProperty(EnumEmailServerProperties.STORE_PROTOCOL
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.TRANSPORT_PROTOCOL
				.getPropertyName())) {
			this.transportProtocol = props
					.getProperty(EnumEmailServerProperties.TRANSPORT_PROTOCOL
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.USER.getPropertyName())) {
			this.user = props.getProperty(EnumEmailServerProperties.USER
					.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.PASSWORD
				.getPropertyName())) {
			this.password = props
					.getProperty(EnumEmailServerProperties.PASSWORD
							.getPropertyName());
		}
		if (props.containsKey(EnumEmailServerProperties.SMTPAUTH
				.getPropertyName())) {
			this.auth = Boolean.valueOf(props
					.getProperty(EnumEmailServerProperties.SMTPAUTH
							.getPropertyName()));
		} else if (props.containsKey(EnumEmailServerProperties.SMTPAUTH_ALIAS
				.getPropertyName())) {
			this.auth = Boolean.valueOf(props
					.getProperty(EnumEmailServerProperties.SMTPAUTH_ALIAS
							.getPropertyName()));
		}
	}

	/**
	 * 完整构造函数
	 * 
	 * @param props
	 *            邮件服务器配置属性信息
	 */
	public EmailServer(Properties props) {
		this.init(props);
	}

	/**
	 * 转换成sun的JavaMail标准的属性集
	 * 
	 * @return JavaMail标准的属性集
	 */
	public Properties toProperties() {
		Properties props = new Properties();
		if (this.debug == null) {
			props.setProperty(
					EnumEmailServerProperties.DEBUG.getPropertyName(), "false");
		} else {
			props.setProperty(
					EnumEmailServerProperties.DEBUG.getPropertyName(),
					this.debug.toString());
		}
		if (this.mimeAddressStrict != null) {
			props.setProperty(EnumEmailServerProperties.MIMEADDRESSSTRICT
					.getPropertyName(), this.mimeAddressStrict.toString());
		}
		if (this.from != null) {
			props.setProperty(EnumEmailServerProperties.FROM.getPropertyName(),
					this.from);
		}
		if (this.protocolClass != null) {
			props.setProperty(EnumEmailServerProperties.PROTOCOL_CLASS
					.getPropertyName(), this.protocolClass);
		}
		if (this.protocolHost != null) {
			props.setProperty(EnumEmailServerProperties.PROTOCOL_HOST
					.getPropertyName(), this.protocolHost);
		}
		if (this.protocolPort != null && this.protocolPort > 0
				&& this.protocolPort < 65535) {
			props.setProperty(EnumEmailServerProperties.PROTOCOL_PORT
					.getPropertyName(), this.protocolPort.toString());
		}
		if (this.protocolUser != null) {
			props.setProperty(EnumEmailServerProperties.PROTOCOL_USER
					.getPropertyName(), this.protocolUser);
		}
		if (this.smtpHost != null) {
			props.setProperty(EnumEmailServerProperties.SMTPHOST
					.getPropertyName(), this.smtpHost);
			props.setProperty(EnumEmailServerProperties.SMTPHOST_ALIAS
					.getPropertyName(), this.smtpHost);
		}
		if (this.smtpPort != null && this.smtpPort > 0 && this.smtpPort < 65535) {
			props.setProperty(EnumEmailServerProperties.SMTPPORT
					.getPropertyName(), this.smtpPort.toString());
			props.setProperty(EnumEmailServerProperties.SMTPPORT_ALIAS
					.getPropertyName(), this.smtpPort.toString());
		}
		if (this.storeProtocol != null) {
			props.setProperty(EnumEmailServerProperties.STORE_PROTOCOL
					.getPropertyName(), this.storeProtocol);
		}
		if (this.transportProtocol != null) {
			props.setProperty(EnumEmailServerProperties.TRANSPORT_PROTOCOL
					.getPropertyName(), this.transportProtocol);
		}
		if (this.user != null) {
			props.setProperty(EnumEmailServerProperties.USER.getPropertyName(),
					this.user);
		}
		if (this.password != null) {
			props.setProperty(EnumEmailServerProperties.PASSWORD
					.getPropertyName(), this.password);
		}
		if (this.auth != null) {
			props.setProperty(EnumEmailServerProperties.SMTPAUTH
					.getPropertyName(), this.auth.toString());
			props.setProperty(EnumEmailServerProperties.SMTPAUTH_ALIAS
					.getPropertyName(), this.auth.toString());
		}
		return props;
	}

	/**
	 * @return the smtpHost
	 */
	public String getSmtpHost() {
		return smtpHost;
	}

	/**
	 * @param smtpHost
	 *            the smtpHost to set
	 */
	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	/**
	 * @return the smtpPort
	 */
	public Integer getSmtpPort() {
		return smtpPort;
	}

	/**
	 * @param smtpPort
	 *            the smtpPort to set
	 */
	public void setSmtpPort(Integer smtpPort) {
		this.smtpPort = smtpPort;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * @param from
	 *            the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @return the debug
	 */
	public Boolean getDebug() {
		return debug;
	}

	/**
	 * @param debug
	 *            the debug to set
	 */
	public void setDebug(Boolean debug) {
		this.debug = debug;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * @return the mimeAddressStrict
	 */
	public Boolean getMimeAddressStrict() {
		return mimeAddressStrict;
	}

	/**
	 * @param mimeAddressStrict
	 *            the mimeAddressStrict to set
	 */
	public void setMimeAddressStrict(Boolean mimeAddressStrict) {
		this.mimeAddressStrict = mimeAddressStrict;
	}

	/**
	 * @return the protocolClass
	 */
	public String getProtocolClass() {
		return protocolClass;
	}

	/**
	 * @param protocolClass
	 *            the protocolClass to set
	 */
	public void setProtocolClass(String protocolClass) {
		this.protocolClass = protocolClass;
	}

	/**
	 * @return the protocolHost
	 */
	public String getProtocolHost() {
		return protocolHost;
	}

	/**
	 * @param protocolHost
	 *            the protocolHost to set
	 */
	public void setProtocolHost(String protocolHost) {
		this.protocolHost = protocolHost;
	}

	/**
	 * @return the protocolPort
	 */
	public Integer getProtocolPort() {
		return protocolPort;
	}

	/**
	 * @param protocolPort
	 *            the protocolPort to set
	 */
	public void setProtocolPort(Integer protocolPort) {
		this.protocolPort = protocolPort;
	}

	/**
	 * @return the protocolUser
	 */
	public String getProtocolUser() {
		return protocolUser;
	}

	/**
	 * @param protocolUser
	 *            the protocolUser to set
	 */
	public void setProtocolUser(String protocolUser) {
		this.protocolUser = protocolUser;
	}

	/**
	 * @return the storeProtocol
	 */
	public String getStoreProtocol() {
		return storeProtocol;
	}

	/**
	 * @param storeProtocol
	 *            the storeProtocol to set
	 */
	public void setStoreProtocol(String storeProtocol) {
		this.storeProtocol = storeProtocol;
	}

	/**
	 * @return the transportProtocol
	 */
	public String getTransportProtocol() {
		return transportProtocol;
	}

	/**
	 * @param transportProtocol
	 *            the transportProtocol to set
	 */
	public void setTransportProtocol(String transportProtocol) {
		this.transportProtocol = transportProtocol;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((debug == null) ? 0 : debug.hashCode());
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime
				* result
				+ ((mimeAddressStrict == null) ? 0 : mimeAddressStrict
						.hashCode());
		result = prime * result
				+ ((protocolClass == null) ? 0 : protocolClass.hashCode());
		result = prime * result
				+ ((protocolHost == null) ? 0 : protocolHost.hashCode());
		result = prime * result
				+ ((protocolPort == null) ? 0 : protocolPort.hashCode());
		result = prime * result
				+ ((protocolUser == null) ? 0 : protocolUser.hashCode());
		result = prime * result
				+ ((smtpHost == null) ? 0 : smtpHost.hashCode());
		result = prime * result
				+ ((smtpPort == null) ? 0 : smtpPort.hashCode());
		result = prime * result
				+ ((storeProtocol == null) ? 0 : storeProtocol.hashCode());
		result = prime
				* result
				+ ((transportProtocol == null) ? 0 : transportProtocol
						.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		EmailServer other = (EmailServer) obj;
		if (debug == null) {
			if (other.debug != null)
				return false;
		} else if (!debug.equals(other.debug))
			return false;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (mimeAddressStrict == null) {
			if (other.mimeAddressStrict != null)
				return false;
		} else if (!mimeAddressStrict.equals(other.mimeAddressStrict))
			return false;
		if (protocolClass == null) {
			if (other.protocolClass != null)
				return false;
		} else if (!protocolClass.equals(other.protocolClass))
			return false;
		if (protocolHost == null) {
			if (other.protocolHost != null)
				return false;
		} else if (!protocolHost.equals(other.protocolHost))
			return false;
		if (protocolPort == null) {
			if (other.protocolPort != null)
				return false;
		} else if (!protocolPort.equals(other.protocolPort))
			return false;
		if (protocolUser == null) {
			if (other.protocolUser != null)
				return false;
		} else if (!protocolUser.equals(other.protocolUser))
			return false;
		if (smtpHost == null) {
			if (other.smtpHost != null)
				return false;
		} else if (!smtpHost.equals(other.smtpHost))
			return false;
		if (smtpPort == null) {
			if (other.smtpPort != null)
				return false;
		} else if (!smtpPort.equals(other.smtpPort))
			return false;
		if (storeProtocol == null) {
			if (other.storeProtocol != null)
				return false;
		} else if (!storeProtocol.equals(other.storeProtocol))
			return false;
		if (transportProtocol == null) {
			if (other.transportProtocol != null)
				return false;
		} else if (!transportProtocol.equals(other.transportProtocol))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the auth
	 */
	public Boolean getAuth() {
		return auth;
	}

	/**
	 * @param auth
	 *            the auth to set
	 */
	public void setAuth(Boolean auth) {
		this.auth = auth;
	}
}
